<?php

namespace xingchen\vue;

use yii\web\View;
use yii\helpers\ArrayHelper;
use yii\base\Application;
use yii\base\BootstrapInterface;
use xingchen\vue\gii\Generator;

/**
 * @author lishipeng <buptlsp@sina.com>
 */
class Bootstrap implements BootstrapInterface
{
    /**
     * Bootstrap method to be called during application bootstrap stage.
     *
     * @param Application $app the application currently running
     */
    public function bootstrap($app)
    {
        if (ArrayHelper::getValue($app->getComponents(), 'view.class') === View::class) {
            $app->set('view', [
                'class' => 'xingchen\vue\View',
                'renderers' => [
                    'api' => [
                        'class' => 'xingchen\vue\ViewRenderer',
                    ],
                ],
            ]);
        }
        if ($app->hasModule('gii')) {
            $app->getModule('gii')->generators = array_merge($app->getModule('gii')->generators, [
                'api' => [
                    'class' => 'xingchen\vue\generators\Generator',
                    'templates' => [
                        'default' => '@xingchen/vue/generators/template/api',
                    ],
                ],
            ]);
        }
        if ($app->hasModule('gii')) {
            $config = [
                'vueCrud' => [
                    'class' => Generator::class,
                    'templates' => [
                        'default' => '@xingchen/vue/gii/default',
                    ],
                ],
                'vueAjaxCrud' => [
                    'class' => Generator::class,
                    'templates' => [
                        'default' => '@xingchen/vue/gii/ajaxCrud',
                    ],
                ],
            ];
            $app->getModule('gii')->generators = array_merge($app->getModule('gii')->generators, $config);
        }
    }
}
